<template>
  <div>
    <h1>图片上传</h1>
    <form @submit.prevent="uploadImages">
      <div>
        <label for="imageFolder">选择文件夹或多个图片:</label>
        <input type="file" id="imageFolder" @change="onFilesSelected" webkitdirectory directory multiple
          accept="image/*">
      </div>
      <!-- <div>
        <label for="position">位置:</label>
        <select id="position" v-model="position">
          <option value="TYeye">眼睛</option>
          <option value="palm">手掌</option>
          <option value="tongue">舌头</option>
        </select>
      </div> -->
      <button type="submit">上传图片</button>
    </form>
  </div>
</template>


<script>
import axios from 'axios';

export default {
  data() {
    return {
      selectedFiles: [],
      position: 'TYeye',
      url: '/medical/lable/upload'
    };
  },
  methods: {
    onFilesSelected(event) {
      this.selectedFiles = Array.from(event.target.files);
    },
    async uploadImages() {
      if (!this.selectedFiles.length) {
        alert('请选择至少一个文件');
        return;
      }

      for (const file of this.selectedFiles) {
        const formData = new FormData();
        formData.append('file', file);
        formData.append('photoName', file.name);
        formData.append('position', this.position);
        formData.append('tagType', 'palm');

        try {
          const response = await axios.post(`${this.URLhost}${this.url}`, formData, {
            headers: {
              'Content-Type': 'multipart/form-data'
            }
          });
          console.log(`文件 ${file.name} 上传成功!`, response.data);
        } catch (error) {
          console.error(`文件 ${file.name} 上传失败:`, error);
          alert(`文件 ${file.name} 上传失败，请重试。`);
        }
      }
      alert('所有图片上传完成！');
    }
  }
};
</script>

<style scoped>
/* 添加一些基本样式 */
div {
  margin-bottom: 10px;
}

label {
  display: block;
  margin-bottom: 5px;
}

input,
select {
  width: 20%;
  padding: 8px;
  box-sizing: border-box;
}

button {
  padding: 10px 20px;
  background-color: #007BFF;
  color: white;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}
</style>